<template>
	<view class="memo_list">
		<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
			<view style="text-align: center;">{{ date }}</view>
		</picker>
		<uni-list>
			<view v-for="item in memoList" class="list_item" bindtap="click" :key="item.memoId"
				@click="showDetail(item)">
				<!-- 包裹两行文字 -->
				<view class="item_info">
					<text>{{ item.memoTitle }}</text>
					<text class="text_small">{{ item.memoText }}</text>
				</view>
			</view>
		</uni-list>
		<uni-load-more :status="statusLoadMore" />
		<uni-fab horizontal="right" icon="add" @fabClick="addMemo" style="opacity: 0.5;"></uni-fab>
	</view>
</template>

<script>

export default {
	data() {
		return {
			date: this.getDate(),
			statusLoadMore: "more",
			memoList: [],
			page: {
				current: 1,
				size: 5
			},
			pages: 0
		}
	},
	computed: {
		startDate() {
			return this.getDate('start');
		},
		endDate() {
			return this.getDate('end');
		}
	},
	onShow() {
		this.reset()
	},
	methods: {
		async getMemoList() {
			let result = await this.$API.memo.getMemoList({ page: this.page, memoDate: this.date })
			if (result.code == 200) {
				this.memoList = this.memoList.concat(result.data.records)
				this.pages = result.data.pages
				if (this.page.current >= this.pages) {
					this.statusLoadMore = "nomore"
				} else {
					this.statusLoadMore = "more"
				}
			}
		},
		reset() {
			this.page.current = 1
			this.memoList = []
			this.getMemoList()
		},
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();

			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},
		bindDateChange: function (e) {
			this.date = e.detail.value
			this.reset()
		},
		addMemo() {
			uni.setStorageSync('memoDate', this.date);
			uni.navigateTo({
				url: '/pages/memo/add/index'
			})
		},
		showDetail(item) {
			let newItem = JSON.parse(JSON.stringify(item))
			for(let key in newItem) {
				if (newItem[key] === null) {
					newItem[key] = ''
				}
			}
			uni.setStorageSync('memo', newItem)
			uni.navigateTo({
				url: '/pages/memo/detail/index'
			})
		},
		/**
		 * 下拉刷新回调函数
		 */
		async onPullDownRefresh() {
			this.reset()
			uni.showToast({
				title: '刷新成功',
				icon: 'none',
				duration: 2000
			})
			//隐藏转圈动画
			uni.stopPullDownRefresh()
		},
		/**
		 * 上拉加载更多
		 */
		onReachBottom() {
			// 显示加载中
			if (this.statusLoadMore == "nomore") return
			this.statusLoadMore = "loading"
			this.page.current++
			this.getMemoList()
		},
	}
}
</script>

<style lang="scss" scoped>
.memo_list {
	width: 100%;
	height: 100%;

	.list_item {
		height: 120rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		border-radius: 18rpx;
		border: 1rpx solid #E0E3DA;
		box-shadow: 2rpx 6rpx 0rpx #e5e8df;
		background-color: #ffffff;
		margin: 30rpx;
		padding: 25rpx;

	}

	.item_info {
		width: 100%;
		margin-left: 25rpx;
		display: flex;
		flex-direction: column;
	}

	.text_small {
		font-size: 30rpx;
		word-break: break-all;
		color: #7a7878;
		margin-top: 10rpx
	}
}
</style>
